package chapter_13_proxy.dynamic_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;

/**
 * Created by CharloYao on 2017/10/11.
 * 动态代理类工厂
 */
public class DynamicProxyFactory {

    /**
     * 客户类调用次工厂方法获得代理对象
     * 对客户类来说并不知道返回的是 代理对象 还是 委托对象；
     * @return
     */
    public static Subject getInstance(){
        Subject subject = new RealSubject();
        InvocationHandler handler = new SubjectInvocationHandler(subject);

        Subject proxy = null;

        proxy = (Subject) Proxy.newProxyInstance(
                subject.getClass().getClassLoader(),
                subject.getClass().getInterfaces(),
                handler);

        return proxy;
    }
}
